/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import dk.lindhardt.gwt.geie.shared.CellFormat;

/**
 * User: AnAmuser
 * Date: 17-08-11
 * <p/>
 * Styles an element from a {@link CellFormat} by setting appropriate DOM properties on the element.
 */
public class DefaultElementStyler implements ElementStyler {

   /**
    * Styles an element from a {@link CellFormat} by setting appropriate DOM properties on the element
    * @param element the element to be styled
    * @param cellFormat the cell format
    */
   public void styleElement(Element element, CellFormat cellFormat) {
      DOM.setStyleAttribute(element, "backgroundColor", cellFormat.getBackgroundColor().getHexValue());
      DOM.setStyleAttribute(element, "color", cellFormat.getColor().getHexValue());

      if (cellFormat.getBorders().size() > 0) {
         styleBorder(element, cellFormat);
      }
      styleFont(element, cellFormat);
      setAlignment(element, cellFormat);
   }

   private void setAlignment(Element element, CellFormat cellFormat) {
      HasHorizontalAlignment.HorizontalAlignmentConstant hAlign = null;
      if (cellFormat.getAlignment() != null) {
         switch (cellFormat.getAlignment()) {
            case LEFT:
               hAlign = HasHorizontalAlignment.ALIGN_LEFT;
               break;
            case CENTER:
               hAlign = HasHorizontalAlignment.ALIGN_CENTER;
               break;
            case RIGHT:
               hAlign = HasHorizontalAlignment.ALIGN_RIGHT;
               break;
         }
      }
      HasVerticalAlignment.VerticalAlignmentConstant vAlign = null;
      if (cellFormat.getValignment() != null) {
         switch (cellFormat.getValignment()) {
            case BOTTOM:
               vAlign = HasVerticalAlignment.ALIGN_BOTTOM;
               break;
            case CENTER:
               vAlign = HasVerticalAlignment.ALIGN_MIDDLE;
               break;
            case TOP:
               vAlign = HasVerticalAlignment.ALIGN_TOP;
               break;
         }
      }
      if (hAlign != null) {
         DOM.setElementProperty(element, "align", hAlign.getTextAlignString());
      } else {
         DOM.setElementProperty(element, "align", HasHorizontalAlignment.ALIGN_DEFAULT.getTextAlignString());
      }
      if (vAlign != null) {
         DOM.setStyleAttribute(element, "verticalAlign", vAlign.getVerticalAlignString());
      } else {
         DOM.setStyleAttribute(element, "verticalAlign", HasVerticalAlignment.ALIGN_BOTTOM.getVerticalAlignString());
      }
   }

   private void styleFont(Element element, CellFormat cellFormat) {
      DOM.setStyleAttribute(element, "fontWeight", cellFormat.isBold() ? "bold" : "normal");
      DOM.setStyleAttribute(element, "fontStyle", cellFormat.isItalic() ? "italic" : "normal");
      DOM.setStyleAttribute(element, "textDecoration", cellFormat.isUnderlined() ? "underline" : "none");
      if (cellFormat.getFont() != null) {
         switch (cellFormat.getFont()) {
            case ARIAL:
               DOM.setStyleAttribute(element, "fontFamily", "Arial, sans-serif");
               break;
            case TAHOMA:
               DOM.setStyleAttribute(element, "fontFamily", "Tahoma, sans-serif");
               break;
            case TIMES:
               DOM.setStyleAttribute(element, "fontFamily", "'Times New Roman', serif");
         }
      }
      DOM.setStyleAttribute(element, "fontSize", String.valueOf(cellFormat.getFontSize()));
   }

   private void styleBorder(Element element, CellFormat cellFormat) {
      for (CellFormat.Border border : cellFormat.getBorders()) {
         int borderWidth = cellFormat.getBorderWidth(border);
         String borderStyle = getBorderStyle(border, cellFormat);
         switch (border) {
            case ALL:
               DOM.setStyleAttribute(element, "border", borderWidth + "px " + borderStyle);
               break;
            case BOTTOM:
               DOM.setStyleAttribute(element, "borderBottom", borderWidth + "px " + borderStyle);
               break;
            case TOP:
               DOM.setStyleAttribute(element, "borderTop", borderWidth + "px " + borderStyle);
               break;
            case LEFT:
               DOM.setStyleAttribute(element, "borderLeft", borderWidth + "px " + borderStyle);
               break;
            case RIGHT:
               DOM.setStyleAttribute(element, "borderRight", borderWidth + "px " + borderStyle);
               break;
         }
      }
   }

   private String getBorderStyle(CellFormat.Border border, CellFormat cellFormat) {
      return cellFormat.getBorderType(border).name().toLowerCase();
   }
}
